package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.CustomException;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;


    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage, pageSize);

        Page<CheckItem> checkItems = checkItemDao.queryPage(queryString);

        return new PageResult(checkItems.getTotal(), checkItems.getResult());
    }

    /**
     * 新增
     *
     * @param checkItem
     */
    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(int id) {
        CheckItem list = checkItemDao.getById(id);
        //如果检查项被检查组使用则不能删除，并提示：
            String code = list.getCode();
            List<CheckGroup> groupList = checkGroupDao.getByCode(code);
            if (groupList.size() >0 ) {
                throw new CustomException("当前检查项已经被关联到检查组，不允许删除");
            }

        checkItemDao.deleteId(id);
    }

    /**
     * 编辑回显
     * @param id
     */
    @Override
    public CheckItem editById(int id) {
        CheckItem emp = checkItemDao.getById(id);

        return emp;

    }

    //编辑
    @Override
    public boolean edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
        return  true;
    }
//查找全部
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
